﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public abstract class BaseEnergyWidget : CanvasWidget
    {
        public RectangleWidget rectangleWidget = new RectangleWidget() { FillColor = Color.Gray, OutlineColor = Color.White, IsHitTestVisible = false };
        public LabelWidget titleWidget = new LabelWidget() { Color = Color.White, HorizontalAlignment = WidgetAlignment.Center, Margin = new Vector2(0, 5), FontScale = 0.7f };
        public LabelWidget bottomWidget = new LabelWidget() { Color = Color.White, HorizontalAlignment = WidgetAlignment.Center, Margin = new Vector2(0, 5), FontScale = 0.7f };
        public System.Action Tick;
        //能量条进度
        public MekProgressWidget mekProgress = new MekProgressWidget(MekProgressWidget.ProgessType.progress2) { Size = new Vector2(8, 100) };
        public StackPanelWidget StackRight = new StackPanelWidget() { Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Far, VerticalAlignment = WidgetAlignment.Center };
        public CanvasWidget CanvasMain = new CanvasWidget() { Size = new Vector2(400, 340) };
        public CanvasWidget CanvasRight = new CanvasWidget() { Size = new Vector2(220, 340), HorizontalAlignment = WidgetAlignment.Far, VerticalAlignment = WidgetAlignment.Near, Margin = new Vector2(5, 0) };
        public CanvasWidget canvasWidgetSplit = new CanvasWidget() { Margin = new Vector2(-15, 0) };
        public RectangleWidget rectangleSplit = new RectangleWidget() { FillColor = Color.White, OutlineColor = Color.Transparent };
        public ImageWidget Overlay = new ImageWidget() { Size = new Vector2(36f), Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Tabs/energy_info") };
        public MoreCanvasWidget MoreBox = new MoreCanvasWidget() { };
        public BevelledRectangleWidget MoreRect = new BevelledRectangleWidget() { BevelColor = Color.White, CenterColor = Color.Black };
        public LabelWidget MoreText = new LabelWidget() { Color = Color.Green, FontScale = 0.7f };

        public BaseEnergyWidget(ComponentMiner componentMiner)
        {
            Size = new Vector2(650, 340);
            canvasWidgetSplit.Size = new Vector2(2, Size.Y);
            canvasWidgetSplit.AddChildren(rectangleSplit);
            HorizontalAlignment = WidgetAlignment.Center;
            VerticalAlignment = WidgetAlignment.Center;
            //背景
            Children.Add(rectangleWidget);
            //绘制主界面
            Children.Add(CanvasMain);

            //机器界面能量条
            //机器界面
            CanvasMain.AddChildren(titleWidget);
            CanvasMain.AddChildren(mekProgress);
            mekProgress.HorizontalAlignment = WidgetAlignment.Far;
            mekProgress.VerticalAlignment = WidgetAlignment.Center;
            //背包
            CanvasRight.AddChildren(StackRight);
            CanvasRight.AddChildren(canvasWidgetSplit);
            AddChildren(CanvasRight);
            InitBackPack(componentMiner.Inventory);
            InitMoreInfo();
        }
        public virtual void InitMoreInfo()
        {
            MoreBox.Children.Add(MoreRect);
            MoreBox.Children.Add(MoreText);
            MoreBox.VerticalAlignment = WidgetAlignment.Far;
            MoreBox.Children.Add(Overlay);
            MoreBox.Size = Overlay.Size;
            MoreBox.Margin = new Vector2(-Overlay.Size.X, 0);
            MoreRect.IsVisible = false;
            MoreText.IsVisible = false;
            Children.Add(MoreBox);
            MoreBox.Action = () =>
            {
                if (Overlay.IsVisible)
                {
                    MoreRect.IsVisible = true;
                    MoreText.IsVisible = true;
                    float W = 0f;
                    string[] d = MoreText.Text.Split(new char[] { '\n' }, System.StringSplitOptions.None);
                    for (int i = 0; i < d.Length; i++)
                    {
                        float w = 0f;
                        for (int j = 0; j < d[i].Length; j++)
                        {
                            char c = d[i][j];
                            w += MoreText.Font.GetGlyph(c).Width * MoreText.FontScale + MoreText.Font.Spacing.X;
                        }
                        W = MathUtils.Max(w, W);
                    }

                    MoreText.Size = new Vector2(W, MoreText.CalculateLineHeight() * d.Length);

                    Overlay.IsVisible = false;
                    MoreText.HorizontalAlignment = WidgetAlignment.Center;
                    MoreText.VerticalAlignment = WidgetAlignment.Center;
                    MoreBox.Size = MoreText.Size + new Vector2(4f, 4f);
                    MoreBox.Margin = new Vector2(-MoreBox.Size.X, 0);
                }
                else
                {
                    MoreRect.IsVisible = false;
                    MoreText.IsVisible = false;
                    Overlay.IsVisible = true;
                    MoreBox.Size = Overlay.Size;
                    MoreBox.Margin = new Vector2(-Overlay.Size.X, 0);
                }
            };
        }
        public void setTitle(string txt)
        {
            titleWidget.Text = txt;
        }
        public void SetPowerProgress(float progress)
        {
            if (progress < 0f) progress = 0f;
            if (progress > 1f) progress = 1f;
            mekProgress.SetProgress(progress);
        }
        /// <summary>
        /// 设置左下角点击详情
        /// </summary>
        /// <param name="text"></param>
        public void SetLeftBottomMoreInfo(string text)
        {
            MoreText.Text = text;
        }
        public void InitBackPack(IInventory inventory)
        {
            LabelWidget labelWidget = new LabelWidget() { Text = "背包", HorizontalAlignment = WidgetAlignment.Center, Margin = new Vector2(0, 15) };
            StackRight.Children.Add(labelWidget);
            int num = 10;
            for (int i = 0; i < 4; i++)
            {
                StackPanelWidget stackLine = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Near };
                for (int j = 0; j < 4; j++)
                {
                    InventorySlotWidget slotWidget = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(4, 2), BevelColor = Color.Gray };
                    slotWidget.AssignInventorySlot(inventory, num++);
                    stackLine.Children.Add(slotWidget);
                }
                StackRight.Children.Add(stackLine);
            }
        }
        public override void Update()
        {
            Tick?.Invoke();
        }
    }
}
